package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能简述:
 *
 * @author 86156
 * @date: 2024/09/05
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> setmealList = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(setmealList.getTotal(),setmealList.getResult());
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 向套餐表中添加数据，并且获取返回的主键id
        setmealMapper.insert(setmeal);
        // 向套餐菜品关系表中添加数据
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        // 设置套餐菜品关系表中的套餐id
        setmealDishList.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertBatch(setmealDishList);
    }

    /**
     * 套餐启售停售
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        setmealMapper.updateStatus(id, status);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 更新基本信息
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
        // 删除原有的套餐菜品关系数据
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        // 插入新的套餐菜品数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        // 根据id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVo = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVo);
        // 根据套餐id查询菜品id列表：套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        // 封装套餐VO
        setmealVo.setSetmealDishes(setmealDishes);
        return setmealVo;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        setmealMapper.deleteBatch(ids);
        setmealDishMapper.deleteBySetmealIDs(ids);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
